import tkx


class Calculator(tkx.TkxWindow):

    def __init__(self, xml: str):
        super().__init__(xml)
        self.up_value = 0
        self.value = 0
        self.op = '='
        self.update_down_num()

    def update_down_num(self):
        self.set_value('down_num', f'{self.op} {self.value}')

    def add_num(self, num: int):
        self.value *= 10
        self.value += num
        self.update_down_num()

    def new_operate(self, operate: str):
        if self.op == '=':
            self.up_value = self.value
        elif self.op == '+':
            self.up_value += self.value
        elif self.op == '-':
            self.up_value -= self.value
        elif self.op == '*':
            self.up_value *= self.value
        elif self.op == '/':
            self.up_value /= self.value
        self.value = 0
        self.op = operate
        self.update_down_num()
        self.set_value('up_num', self.up_value)


with open('./calculator.xml', 'r', encoding='utf-8') as f:
    c = Calculator(f.read())
    c.show()
